//
//  JZGTool.m
//  WLW
//
//  Created by TeamMac on 2017/10/19.
//  Copyright © 2017年 liutonglin. All rights reserved.
//

#import "JZGTool.h"
#import "sys/utsname.h"

@implementation JZGTool

// 字符串转10位时间戳
+ (NSString *) stringToTimeIntervalBitIOS:(NSString *)timeString {
    JZGTool *tool = [[JZGTool alloc] init];
    return [tool stringToTimeInterval:timeString];
}

// 判断服务器获取到的时间戳是否大于当前时间戳
+ (BOOL) isServerTimeGreaterNow:(NSString *)serverIntervalString {
    
    NSDate *nowDate = [NSDate date];
    NSTimeInterval nowInterval = [nowDate timeIntervalSince1970];
    
    if ([serverIntervalString doubleValue]/1000 - nowInterval > 0) {
        return YES;
    }
    else {
        return NO;
    }
}

// 返回yyyy-MM-dd  HH:mm:ss
// 将服务器获取的时间戳转化为本地要显示的字符串(用于mycooking中的显示)
+ (NSString *) showFromServerIntervalString:(NSString *)serverIntervalString {
    
    NSDate *serverDate = [NSDate dateWithTimeIntervalSince1970:[serverIntervalString doubleValue]];
    
    // 获得服务器时间的年月日
    NSDateFormatter *serverFormatter = [[NSDateFormatter alloc] init];
    [serverFormatter setDateFormat:@"yyyy-MM-dd  HH:mm:ss"];
    NSString *serverTimeString = [serverFormatter stringFromDate:serverDate];
    
    
    return serverTimeString; // 返回年月日时分秒
//    
//    // 获得当前时间的年月日
//    NSDateFormatter *nowFormatter = [[NSDateFormatter alloc] init];
//    [nowFormatter setDateFormat:@"yyyy-MM-dd"];
//    NSString *nowTimeString = [serverFormatter stringFromDate:[NSDate date]];
//    
//    if ([serverTimeString isEqualToString:nowTimeString]) {
//        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//        [formatter setDateFormat:@"HH:mm"];
//        NSString *timeString = [formatter stringFromDate:serverDate];
//        // 返回不带年月日的时间字符串
//        return timeString;
//    }
//    else {
//        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//        [formatter setDateFormat:@"MM-dd HH:mm"];
//        NSString *timeString = [formatter stringFromDate:serverDate];
//        // 返回带月日的时间字符串
//        return timeString;
//    }
}

// 返回yyyy-MM-dd
// 将服务器获取的10位时间戳转化为本地要显示的字符串
+ (NSString *) stringFromServerIntervalString:(NSString *)serverIntervalString {
    
    NSDate *serverDate = [NSDate dateWithTimeIntervalSince1970:[serverIntervalString doubleValue]];
    
    // 获得服务器时间的年月日
    NSDateFormatter *serverFormatter = [[NSDateFormatter alloc] init];
    [serverFormatter setDateFormat:@"yyyy-MM-dd"];
    
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateFormat:@"MM-dd HH:mm"];
    
    NSString *timeString = [serverFormatter stringFromDate:serverDate];
    // 返回带月日的时间字符串
    return timeString;
    
}


// 字符串转时间戳
- (NSString *) stringToTimeInterval:(NSString *)timeString {
    
    // 加上时分秒-----货源发布模块"按需取","需送货",需要一个晚于当前时间的时间
    //NSString *ATimeString = [NSString stringWithFormat:@"%@ 23:59:59",timeString];
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 2017-10-13 21:09:00
    [formatter setDateFormat:@"yyyy-MM-dd"]; // 2017-10-13
    // 字符串转时间
    NSDate *date = [formatter dateFromString:timeString];
    // 时间转时间戳
    NSTimeInterval interval = [date timeIntervalSince1970];
    NSString *intervalString = [NSString stringWithFormat:@"%.f",interval];
    
    return intervalString;
}

/**
 * 根据响应连获取当前视图所在的控制器
 */
+ (UIViewController *)getCurrentController
{
    UIViewController *result = nil;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [UIApplication sharedApplication].windows;
        for (UIWindow *temp in windows) {
            if (temp.windowLevel == UIWindowLevelNormal) {
                window = temp;
                break;
            }
        }
    }
    id nextResponder = nil;
    UIViewController *appRootVc = window.rootViewController;
    if (appRootVc.presentedViewController) {
        nextResponder = appRootVc.presentedViewController;
    }else{
        UIView *frontView = [[window subviews] objectAtIndex:0];
        nextResponder = [frontView nextResponder];
    }
    
    if ([nextResponder isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabbar = (UITabBarController *)nextResponder;
        UINavigationController *nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
        result = nav.childViewControllers.lastObject;
    }else if([nextResponder isKindOfClass:[UINavigationController class]]){
        UIViewController *nav = (UIViewController *)nextResponder;
        result = nav.childViewControllers.lastObject;
    }else{
        result = nextResponder;
    }
    return result;
}

+ (UIViewController *)getCurrentVC{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    return currentVC;
}

+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC{
    UIViewController *currentVC;
    if ([rootVC presentedViewController]) {
        rootVC = [rootVC presentedViewController];
    }
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        currentVC = rootVC;
    }
    return currentVC;
}

+ (UIViewController *)topViewController {
    
    JZGTool *tool = [[JZGTool alloc] init];
    
    UIViewController *resultVC;
    resultVC = [tool _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [tool _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

// 贝塞尔曲线画圆
+ (UIImageView *)BezierDrawCircleImageName:(NSString *)imageName {
   // UIImageView *imageView = [[UIImageView alloc] initWithImage:ImageNamed(imageName)];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    //开始对imageView进行画图
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
    //使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
    [imageView drawRect:imageView.bounds];
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    //结束画图
    UIGraphicsEndImageContext();
    
    return imageView;
}

/// 贝塞尔曲线画圆(传入图片控件)
+ (void)BezierDrawCircleImageView:(UIImageView *)imageView {
    //开始对imageView进行画图
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
    //使用贝塞尔曲线画出一个圆形图
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds cornerRadius:imageView.frame.size.width] addClip];
    [imageView drawRect:imageView.bounds];
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    //结束画图
    UIGraphicsEndImageContext();
}

// 类名转类类型
+ (Class)getClassClass:(NSString *)classNameString {
    NSString *ControllerNameString = classNameString;
    const char* ControllerName = [ControllerNameString UTF8String];
    Class class = objc_getClass(ControllerName);
    return class;
}

/// 任意圆角处理
+ (void)bezierPathRadiusWithView:(UIView *)view UIRectCorner:(UIRectCorner)rectCorner radius:(CGFloat)radius {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
}


/// 简单确认弹框
+ (void)OkAlertControllerWithTitle:(NSString *)title handler:(OkAlertHandler)handler {

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (handler) {
            handler();
        }
    }];
    [alertController addAction:OKAction];
    [[JZGTool topViewController] presentViewController:alertController animated:YES completion:nil];
}

/// 查找入栈的控制器,返回控制器实例
+ (UIViewController *)fineControllerWithClassName:(NSString *)fineVCName nowVC:(UIViewController *)nowVC {
    for (UIViewController *fineVC in nowVC.navigationController.childViewControllers) {
        Class fineVCClass = [JZGTool getClassClass:fineVCName];
        if ([fineVC isKindOfClass:[fineVCClass class]]) {
            return fineVC;
        }
    }    
    return nil;
}

/// 出栈到指定的控制器
+ (void)popToControllerWithClassName:(NSString *)toVCName nowVc:(UIViewController *)nowVc {
    UIViewController *popVC = [JZGTool fineControllerWithClassName:toVCName nowVC:nowVc];
    [nowVc.navigationController popToViewController:popVC animated:YES];
}

/// 自动生成属性声明的代码
+ (void)propertyCodeWithDictionary:(NSDictionary *)dict
{
    
    if ([dict isKindOfClass:[NSArray class]]) {
        NSArray *array = (NSArray *)dict;
        dict = array[0];
        NSLog(@"===传过来的是数组不是字典");
    }
    
    NSMutableString *strM = [NSMutableString string];
    [strM appendFormat:@"自动生成模型属性:\n"];
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        
        NSString *str;
        
        //NSLog(@"%@",[obj class]);
        
        //str = [NSString stringWithFormat:@"@property (copy, nonatomic) NSString *%@;",key];
        
        
        if ([obj isKindOfClass:NSClassFromString(@"__NSCFString")] || [obj isKindOfClass:NSClassFromString(@"NSTaggedPointerString")] || [obj isKindOfClass:NSClassFromString(@"__NSCFConstantString")]) {
            str = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@;",key];
        }
        if ([obj isKindOfClass:NSClassFromString(@"__NSCFNumber")]) {
            str = [NSString stringWithFormat:@"@property (nonatomic, assign) int %@;",key];
        }
        if ([obj isKindOfClass:NSClassFromString(@"__NSCFArray")]) {
            str = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@;",key];
        }
        if ([obj isKindOfClass:NSClassFromString(@"__NSCFDictionary")]) {
            str = [NSString stringWithFormat:@"@property (nonatomic, copy) NSDictionary *%@;",key];
        }
        if ([obj isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {
            str = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",key];
        }
         
        
        [strM appendFormat:@"%@\n",str];
    }];
    
    NSLog(@"%@",strM);
}

/// 字典转json
+ (NSString*)dataTojsonString:(id)object
{
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:0
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}
/// json转字典
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}


/// 解析卓联接口文档
+ (void)ZL {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"zl" ofType:@"txt"];
    NSString *content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
    NSMutableString *strM = [NSMutableString string];
    
    NSString *str;
    
    NSArray  *A = [content componentsSeparatedByString:@"\n"];
    [strM appendFormat:@"\n\n//自动生成模型属性:%lu\n",(unsigned long)A.count];
    for (NSString *AString in A) {
        NSArray *B = [AString componentsSeparatedByString:@"\t"];
        if (B.count < 3) {
            NSLog(@"生成属性解析失败!,格式不符合");
        }
        else {
            
             if ([B[1] isEqualToString:@"string"]) {
             str = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@; //!<%@",B[0],B[2]];
             }
             if ([B[1] isEqualToString:@"int"]) {
             str = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@; //!<%@",B[0],B[2]];
             }
             if ([B[1] isEqualToString:@"array"]) {
             str = [NSString stringWithFormat:@"@property (nonatomic, copy) NSArray *%@; //!<%@",B[0],B[2]];
            }
            if ([B[1] isEqualToString:@"guid"]) {
                str = [NSString stringWithFormat:@"@property (nonatomic, copy) NSString *%@; //!<%@",B[0],B[2]];
            }
            if ([B[1] isEqualToString:@"bool"]) {
                str = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL *%@; //!<%@",B[0],B[2]];
            }
            
            [strM appendFormat:@"%@\n",str];
        }
    }
    [strM appendFormat:@"\n\n"];
    NSLog(@"%@",strM);
    
}


/// 获取设备型号然后手动转化为对应名称
+ (NSString *)getZLDeviceName
{
    // 需要#import "sys/utsname.h"
//   #warning 题主呕心沥血总结！！最全面！亲测！全网独此一份！！
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone3,3"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceString isEqualToString:@"iPhone5,1"])    return @"iPhone 5";
//    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5 (GSM+CDMA)";
//    if ([deviceString isEqualToString:@"iPhone5,3"])    return @"iPhone 5c (GSM)";
//    if ([deviceString isEqualToString:@"iPhone5,4"])    return @"iPhone 5c (GSM+CDMA)";
//    if ([deviceString isEqualToString:@"iPhone6,1"])    return @"iPhone 5s (GSM)";
//    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone 5s (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"iPhone5,3"])    return @"iPhone 5c";
    if ([deviceString isEqualToString:@"iPhone5,4"])    return @"iPhone 5c";
    if ([deviceString isEqualToString:@"iPhone6,1"])    return @"iPhone 5s";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone 5s";
    
    if ([deviceString isEqualToString:@"iPhone7,1"])    return @"iPhone 6 Plus";
    if ([deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone 6";
    if ([deviceString isEqualToString:@"iPhone8,1"])    return @"iPhone 6s";
    if ([deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone 6s Plus";
    if ([deviceString isEqualToString:@"iPhone8,4"])    return @"iPhone SE";
    // 日行两款手机型号均为日本独占，可能使用索尼FeliCa支付方案而不是苹果支付
//    if ([deviceString isEqualToString:@"iPhone9,1"])    return @"国行、日版、港行iPhone 7";
//    if ([deviceString isEqualToString:@"iPhone9,2"])    return @"港行、国行iPhone 7 Plus";
//    if ([deviceString isEqualToString:@"iPhone9,3"])    return @"美版、台版iPhone 7";
//    if ([deviceString isEqualToString:@"iPhone9,4"])    return @"美版、台版iPhone 7 Plus";
//    if ([deviceString isEqualToString:@"iPhone10,1"])   return @"国行(A1863)、日行(A1906)iPhone 8";
//    if ([deviceString isEqualToString:@"iPhone10,4"])   return @"美版(Global/A1905)iPhone 8";
//    if ([deviceString isEqualToString:@"iPhone10,2"])   return @"国行(A1864)、日行(A1898)iPhone 8 Plus";
//    if ([deviceString isEqualToString:@"iPhone10,5"])   return @"美版(Global/A1897)iPhone 8 Plus";
//    if ([deviceString isEqualToString:@"iPhone10,3"])   return @"国行(A1865)、日行(A1902)iPhone X";
//    if ([deviceString isEqualToString:@"iPhone10,6"])   return @"美版(Global/A1901)iPhone X";
    if ([deviceString isEqualToString:@"iPhone9,1"])    return @"iPhone 7";
    if ([deviceString isEqualToString:@"iPhone9,2"])    return @"iPhone 7 Plus";
    if ([deviceString isEqualToString:@"iPhone9,3"])    return @"iPhone 7";
    if ([deviceString isEqualToString:@"iPhone9,4"])    return @"iPhone 7 Plus";
    if ([deviceString isEqualToString:@"iPhone10,1"])   return @"iPhone 8";
    if ([deviceString isEqualToString:@"iPhone10,4"])   return @"iPhone 8";
    if ([deviceString isEqualToString:@"iPhone10,2"])   return @"iPhone 8 Plus";
    if ([deviceString isEqualToString:@"iPhone10,5"])   return @"iPhone 8 Plus";
    if ([deviceString isEqualToString:@"iPhone10,3"])   return @"iPhone X";
    if ([deviceString isEqualToString:@"iPhone10,6"])   return @"iPhone X";
    
    if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"iPod5,1"])      return @"iPod Touch (5 Gen)";
    
    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceString isEqualToString:@"iPad1,2"])      return @"iPad 3G";
    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2";
    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"iPad2,4"])      return @"iPad 2";
    if ([deviceString isEqualToString:@"iPad2,5"])      return @"iPad Mini (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,6"])      return @"iPad Mini";
    if ([deviceString isEqualToString:@"iPad2,7"])      return @"iPad Mini (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPad3,1"])      return @"iPad 3 (WiFi)";
    if ([deviceString isEqualToString:@"iPad3,2"])      return @"iPad 3 (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPad3,3"])      return @"iPad 3";
    if ([deviceString isEqualToString:@"iPad3,4"])      return @"iPad 4 (WiFi)";
    if ([deviceString isEqualToString:@"iPad3,5"])      return @"iPad 4";
    if ([deviceString isEqualToString:@"iPad3,6"])      return @"iPad 4 (GSM+CDMA)";
    if ([deviceString isEqualToString:@"iPad4,1"])      return @"iPad Air (WiFi)";
    if ([deviceString isEqualToString:@"iPad4,2"])      return @"iPad Air (Cellular)";
    if ([deviceString isEqualToString:@"iPad4,4"])      return @"iPad Mini 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad4,5"])      return @"iPad Mini 2 (Cellular)";
    if ([deviceString isEqualToString:@"iPad4,6"])      return @"iPad Mini 2";
    if ([deviceString isEqualToString:@"iPad4,7"])      return @"iPad Mini 3";
    if ([deviceString isEqualToString:@"iPad4,8"])      return @"iPad Mini 3";
    if ([deviceString isEqualToString:@"iPad4,9"])      return @"iPad Mini 3";
    if ([deviceString isEqualToString:@"iPad5,1"])      return @"iPad Mini 4 (WiFi)";
    if ([deviceString isEqualToString:@"iPad5,2"])      return @"iPad Mini 4 (LTE)";
    if ([deviceString isEqualToString:@"iPad5,3"])      return @"iPad Air 2";
    if ([deviceString isEqualToString:@"iPad5,4"])      return @"iPad Air 2";
    if ([deviceString isEqualToString:@"iPad6,3"])      return @"iPad Pro 9.7";
    if ([deviceString isEqualToString:@"iPad6,4"])      return @"iPad Pro 9.7";
    if ([deviceString isEqualToString:@"iPad6,7"])      return @"iPad Pro 12.9";
    if ([deviceString isEqualToString:@"iPad6,8"])      return @"iPad Pro 12.9";
    if ([deviceString isEqualToString:@"iPad6,11"])    return @"iPad 5 (WiFi)";
    if ([deviceString isEqualToString:@"iPad6,12"])    return @"iPad 5 (Cellular)";
    if ([deviceString isEqualToString:@"iPad7,1"])     return @"iPad Pro 12.9 inch 2nd gen (WiFi)";
    if ([deviceString isEqualToString:@"iPad7,2"])     return @"iPad Pro 12.9 inch 2nd gen (Cellular)";
    if ([deviceString isEqualToString:@"iPad7,3"])     return @"iPad Pro 10.5 inch (WiFi)";
    if ([deviceString isEqualToString:@"iPad7,4"])     return @"iPad Pro 10.5 inch (Cellular)";
    
    if ([deviceString isEqualToString:@"AppleTV2,1"])    return @"Apple TV 2";
    if ([deviceString isEqualToString:@"AppleTV3,1"])    return @"Apple TV 3";
    if ([deviceString isEqualToString:@"AppleTV3,2"])    return @"Apple TV 3";
    if ([deviceString isEqualToString:@"AppleTV5,3"])    return @"Apple TV 4";
    
    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
    
    return deviceString;
}

/// 修改图片颜色
+ (UIImage *)imageWithColor:(UIColor *)color image:(UIImage *)image
{
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGContextClipToMask(context, rect, image.CGImage);
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

/// 颜色转图片
+ (UIImage*)createImageWithColor:(UIColor*)color {
    
    CGRect rect = CGRectMake(0.0f,0.0f,1.0f,1.0f);UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
    return theImage;
    
}

// 选择对话框
//+ (void)showAlertWithVC:(UIViewController *)VC AlertTitle:(NSString *)title Message:(NSString *)message ActionTitle1:(NSString *)actionTitle1 ActionTitle2:(NSString *)actionTitle2 handler:(void (^ __nullable)(UIAlertAction *action))handler {
//    //    if (title.length == 0) {
//    //        title = @"温馨提示";
//    //    }
//    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
//    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:actionTitle1 style:UIAlertActionStyleDefault handler:nil];
//    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:actionTitle2 style:UIAlertActionStyleDefault handler:handler];
//    [alertVC addAction:cancleAction];
//    [alertVC addAction:confirmAction];
//    [VC presentViewController:alertVC animated:YES completion:nil];
//}


+ (void)showAlertWithVC:(UIViewController *)vc alertTitle:(NSString *)title message:(NSString *)message actionTitle1:(NSString *)actionTitle1 actionTitle2:(NSString *)actionTitle2 actionTitle3:(NSString *)actionTitle3 actionTitle4:(NSString *)actionTitle4 handler:(void (^)(UIAlertAction *))handler {
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    //回复
    UIAlertAction *replyAction = [UIAlertAction actionWithTitle:actionTitle1 style:UIAlertActionStyleDefault handler:handler];
    //复制
    UIAlertAction *copyAction = [UIAlertAction actionWithTitle:actionTitle2 style:UIAlertActionStyleDefault handler:handler];
    //举报
    UIAlertAction *reportAction = [UIAlertAction actionWithTitle:actionTitle3 style:UIAlertActionStyleDefault handler:handler];
    //取消
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:actionTitle4 style:UIAlertActionStyleCancel handler:nil];
    
    [alertVC addAction:replyAction];
    [alertVC addAction:copyAction];
    [alertVC addAction:reportAction];
    [alertVC addAction:cancleAction];
    
    [vc presentViewController:alertVC animated:NO completion:nil];
    
    
}
@end
